package com.share.domain.strategy.service.rule.tree.factory.engine.impl;

import com.share.domain.strategy.model.vo.RuleLogicCheckTypeVo;
import com.share.domain.strategy.model.vo.RuleTreeNodeLineVo;
import com.share.domain.strategy.model.vo.RuleTreeNodeVo;
import com.share.domain.strategy.model.vo.RuleTreeVo;
import com.share.domain.strategy.service.rule.tree.ILogicTreeNode;
import com.share.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.share.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 决策树引擎
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {

    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    private final RuleTreeVo ruleTreeVo;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroup, RuleTreeVo ruleTreeVo) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
        this.ruleTreeVo = ruleTreeVo;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVo process(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        DefaultTreeFactory.StrategyAwardVo strategyAwardVo =null;
        //获取基础信息
        String nextNode = ruleTreeVo.getTreeRootRuleNode();
        Map<String, RuleTreeNodeVo> treeNodeMap = ruleTreeVo.getTreeNodeMap();

        RuleTreeNodeVo ruleTreeNode = treeNodeMap.get(nextNode);

        while (null!=nextNode){
            //获取决策节点
            ILogicTreeNode logicTreeNode = logicTreeNodeGroup.get(ruleTreeNode.getRuleKey());
            String ruleValue = ruleTreeNode.getRuleValue();

            // 决策节点计算
                DefaultTreeFactory.TreeActionEntity logicEntity = logicTreeNode.logic(userId, strategyId, awardId,ruleValue,endDateTime);
            RuleLogicCheckTypeVo ruleLogicCheckTypeVo = logicEntity.getRuleLogicCheckType();
            strategyAwardVo = logicEntity.getStrategyAwardVo();
            log.info("决策树引擎【{}】,treeId:{},node:{},code:{}",ruleTreeVo.getTreeName(),ruleTreeVo.getTreeId(),nextNode,ruleLogicCheckTypeVo.getCode());
            //
            nextNode = nextNode(ruleLogicCheckTypeVo.getCode(), ruleTreeNode.getTreeNodeLineVoList());
            ruleTreeNode = treeNodeMap.get(nextNode);
        }
        //返回最终结果
        return strategyAwardVo;
    }

    private String nextNode(String matterValue, List<RuleTreeNodeLineVo> ruleTreeNodeLineVoList){
        if(null==ruleTreeNodeLineVoList||ruleTreeNodeLineVoList.isEmpty())return null;

        for (RuleTreeNodeLineVo nodeLine : ruleTreeNodeLineVoList) {
            if(decisionLogic(matterValue,nodeLine)){
                return nodeLine.getRuleNodeTo();
            }
        }
//        throw new RuntimeException("决策树引擎nextNode计算失败,未找到可执行节点！");
        return null;
    }



    public Boolean decisionLogic(String matterValue, RuleTreeNodeLineVo noLine){
        switch (noLine.getRuleLimitType()){
            case EQUAL:
                return matterValue.equals(noLine.getRuleLimitValue().getCode());
            case GT:
            case GE:
            case LT:
            case LE:
            default:
                return false;
        }
    }


}
